package com.woniuxy.carrental.controller;

import com.woniuxy.carrental.entity.PageBean;
import com.woniuxy.carrental.entity.ResponseResult;
import com.woniuxy.carrental.entity.Supplier;
import com.woniuxy.carrental.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xilei
 * @create 2021-09-29 下午 2:29
 */
@RestController
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    /**
     * 分页查找
     * @param model
     * @param pageIndex
     * @param pageSize
     * @param supplier
     * @return
     */
    @GetMapping("/supplierlist")
    public ResponseResult<PageBean<Supplier>> getAllSupplier(Model model,int pageIndex,int pageSize,Supplier supplier){
//        List<Supplier> supplierList = supplierService.findSupplier();
        List<Supplier> supplierList = supplierService.findSupplierByPage(pageIndex, pageSize);
        for(Supplier supplier1:supplierList){
            System.out.println(supplier1);
        }
        PageBean<Supplier> pageBean = new PageBean<>();
        pageBean.setBeanList(supplierList);
        pageBean.setTotalRecord(supplierService.findSupplier().size());
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        model.addAttribute("suppliers",supplierList);
        return new ResponseResult<>(pageBean, 200, "OK");
    }

    /**
     * 修改供应商
     * @param supplier
     * @return
     */
    @PutMapping("/supplierlist")
    public ResponseResult updateSupplier(@RequestBody Supplier supplier){
        System.out.println(supplier);
        supplierService.updateSupplier(supplier.getSupplier_name(),supplier.getSupplier_address(),supplier.getSupplier_telephone(), supplier.getSupplier_contant(), supplier.getSupplier_id());
        return new ResponseResult(null, 200, "OK");
    }

    /**
     * 添加供应商
     * @param supplier
     * @return
     */
    @PostMapping("/supplierlist")
    public ResponseResult addSupplier(@RequestBody Supplier supplier){
        System.out.println(supplier);
        supplierService.insertSupplier(supplier);
        return new ResponseResult(null, 200, "OK");
    }

    /**
     * 模糊查询
     * @param model
     * @param supplier_name
     * @param supplier_address
     * @param supplier_telephone
     * @param supplier_contant
     * @param pageIndex
     * @param pageSize
     * @param supplier
     * @return
     */
    @GetMapping("/supplierlist1")
    public ResponseResult<PageBean<Supplier>> getAllSupplierBytiaojian(Model model,String supplier_name, String supplier_address, String supplier_telephone, String supplier_contant, int pageIndex, int pageSize,Supplier supplier){
//        List<Supplier> supplierList = supplierService.findSupplier();
        List<Supplier> supplierList = supplierService.findSupplierByPageByParam(supplier_name, supplier_address, supplier_telephone, supplier_contant, pageIndex, pageSize);
        for(Supplier supplier2:supplierList){
            System.out.println(supplier2);
        }
        PageBean<Supplier> pageBean = new PageBean<>();
        pageBean.setBeanList(supplierList);
        pageBean.setTotalRecord(supplierService.findSupplier().size());
        pageBean.setPageIndex(pageIndex);
        pageBean.setPageSize(pageSize);
        model.addAttribute("suppliers",supplierList);
        return new ResponseResult<>(pageBean, 200, "OK");
    }

    /**
     * 删除供应商
     * @param id
     * @return
     */
    @DeleteMapping("/supplierlist")
    public ResponseResult deleteSupplier(Integer supplier_id){
        System.out.println(supplier_id);
        supplierService.deleteSupplier(supplier_id);
        return new ResponseResult<>(null, 200, "OK");
    }
}
